<template>
  <div class="app-container pay-mange">
    <avue-form
      :option="queryOption"
      v-model="queryParams"
      ref="searchForm"
      v-show="showSearch"
      @keydown.enter="handleQuery"
    >
      <template #menu-form="{ size }">
        <el-button
          icon="Search"
          type="primary"
          :size="size"
          @click="handleQuery"
          >查询</el-button
        >
        <el-button icon="Refresh" :size="size" @click="handleEmpty(searchForm)"
          >重置</el-button
        >
      </template>
    </avue-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="Plus" @click="handleAdd"
          >新增</el-button
        >
        <!-- v-hasPermi="['financial:payManage:add']" -->
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          icon="EditPen"
          plain
          @click="handleUpdate"
          :disabled="disUpdate"
          >编辑</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="Delete"
          plain
          @click="handleDelete"
          :disabled="disDelete"
          >删除</el-button
        >
      </el-col> -->
      <!-- <right-toolbar
        @queryTable="getList"
        v-model:showSearch="showSearch"
      ></right-toolbar> -->
    </el-row>

    <el-table
      v-loading="loading"
      :data="tableList"
      row-key="id"
      stripe
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="30" align="center" />
      <el-table-column prop="number" label="订单编号" width="180">
      </el-table-column>
      <el-table-column prop="orderTitle" label="订单标题" width="200">
        <template #default="scope">
          <el-link
            type="primary"
            :underline="false"
            v-if="scope.row.orderId"
            @click="
              goTo('/sales/indent/salesOrderDetails', {
                salesOrderId: scope.row.orderId
              })
            "
            >{{ scope.row.orderTitle }}</el-link
          >
        </template>
      </el-table-column>
      <el-table-column prop="payDate" label="最近付款日期" width="110">
        <template #default="scope">
          <span>{{ parseTime(scope.row.payDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="allPeriod" label="付款总期次" width="110">
        <template #default="scope">
          <span v-if="scope.row.allPeriod">共{{ scope.row.allPeriod }}期</span>
        </template>
      </el-table-column>
      <el-table-column prop="orderPrice" label="订单总额(元)">
        <template #default="scope">
          <span>{{ scope.row.orderPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="已付款金额(元)">
        <template #default="scope">
          <span>{{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="unPayPrice" label="未付款金额(元)">
        <template #default="scope">
          <span>{{ scope.row.unPayPrice }}</span>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="逾期状态" width="100">
        <template #default="scope">
          <dict-drop :options="overdue_status" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="150"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="View"
            @click="handleDetails(scope.row)"
            >详情</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 人员弹出框下拉框 -->
    <SelectDialog
      :dialog-param="dialogParam"
      @ok="selectDialogData"
      @close="
        val => {
          dialogParam.isDialog = false
        }
      "
    ></SelectDialog>
  </div>
</template>
<script setup name="PayManage">
import { reactive, toRefs, ref } from 'vue'
import { useRouter } from 'vue-router'
import { listPay, delPay } from '@/api/payManage/pay'
import useCommonStore from '@/store/modules/common'
import { getTreeNodeByParam, getPricipalId } from '@/utils/commo'
import {
  companyOption, //所属公司
  departmentOption //部门
} from '@/hooks/dict/order'
const commonStore = useCommonStore()
const props = defineProps({
  payData: {
    type: Object,
    require: false,
    default: () => {
      return {}
    }
  }
})
const emit = defineEmits([])
const router = useRouter()
const { proxy } = getCurrentInstance()
const { overdue_status, pay_type, settle_type } = proxy.useDict(
  'overdue_status', //逾期状态
  'pay_type', //付款方式
  'settle_type' //结算方式
)
const userOption = ref([])
const data = reactive({
  queryParams: {
    pageNum: 1,
    pageSize: 10
  },
  ids: [],
  selectArr: [],
  total: 0,
  queryOption: {
    menuSpan: 5,
    submitBtn: false,
    // gutter: 30,
    emptyBtn: false,
    column: [
      {
        labelWidth: '0',
        prop: 'number',
        span: 3,
        placeholder: '订单编号',
        type: 'input'
      },
      {
        // label: '姓名',
        labelWidth: '0',
        prop: 'status',
        span: 3,
        placeholder: '逾期状态',
        type: 'select',
        dicData: overdue_status
      },
      {
        labelWidth: '0',
        prop: 'deptId',
        span: 3,
        placeholder: '所属公司',
        type: 'tree',
        props: {
          value: 'id', //   value: 'deptId',
          label: 'label', //   label: 'deptName'
          children: 'children'
        },
        dicData: companyOption
      },
      {
        labelWidth: '0',
        prop: 'orderBelongDept',
        span: 3,
        placeholder: '订单所属部门',
        type: 'tree',
        props: {
          value: 'id',
          label: 'label',
          children: 'children'
        },
        dicData: departmentOption
      },
      {
        labelWidth: '0',
        prop: '_orderBelongUser',
        span: 3,
        placeholder: '订单所属人员',
        type: 'select',
        clearable: false,
        props: {
          value: 'uuid'
        },
        popperClass: 'className',
        dicData: userOption,
        multiple: true,
        click: ({ value, column }) => {
          dialogParam.value.type = 'orderBelongUser'
          dialogParam.value.title = '请选择订单所属人员'
          dialogParam.value.isDialog = true
          dialogParam.value.multiple = true
          dialogParam.value.nodeKey = 'uuid'
          dialogParam.value.defaultCheckedKeys =
            queryParams.value._orderBelongUser
          dialogParam.value.dataType = 'person'
        }
      },
      {
        labelWidth: '0',
        prop: 'payPlanDates',
        type: 'daterange',
        span: 3,
        format: 'YYYY-MM-DD',
        valueFormat: 'YYYY-MM-DD',
        startPlaceholder: '付款开始时间',
        endPlaceholder: '付款结束时间'
      }
    ]
  },
  showSearch: true,
  searchDrawer: false /* 高级检索框 */,
  disUpdate: true /* 编辑按钮是否可编辑 */,
  disDelete: true /* 关闭按钮是否可编辑 */,
  loading: false,
  tableList: [],
  productOptions: [],
  dialogParam: {} /* 人员弹窗框参数 */
})
const {
  queryParams,
  ids,
  selectArr,
  total,
  queryOption,
  showSearch,
  searchDrawer,
  disUpdate,
  disDelete,
  loading,
  tableList,
  productOptions,
  dialogParam
} = toRefs(data)
const selectDialogData = val => {
  switch (val.type) {
    case 'orderBelongUser':
      userOption.value = val.selectData
      queryParams.value._orderBelongUser = val.defaultCheckedKeys
      break
  }
}
// 提交
function handleQuery() {
  getList()
}
// 重置
function handleEmpty(formEl) {
  proxy.$refs.searchForm.resetForm()
  getList()
}

// 初始化
async function init() {
  getList()
}
// 获取数据列表
function getList() {
  loading.value = true

  // 订单所属人
  queryParams.value.orderBelongUser = getPricipalId(
    userOption.value,
    queryParams.value._orderBelongUser
  )
  listPay(queryParams.value).then(res => {
    loading.value = false
    tableList.value = res.rows
    total.value = res.total
    console.log(' tableList.value==>', tableList.value)
  })
  console.log('获取数据')
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  selectArr.value = selection
  disUpdate.value = selection.length != 1
  disDelete.value = !selection.length
}

function goTo(url, data) {
  router.push({ path: url, query: data })
}

function handleAdd() {
  goTo('/financial/payForm', {})
}
function handleUpdate() {
  goTo('/financial/payForm', { payId: ids.value[0] })
}
function handleDetails(row) {
  goTo('/financial/payDetails', {
    salesOrderId: row.orderId
  })
}
function handleDelete(row) {
  let rowIds = row.id || ids.value
  console.log('selectArr-->', selectArr.value, 'id==>', rowIds)
  proxy.$modal
    .confirm(
      '是否确认删除名称为"' + selectArr.value.map(e => e.name) + '"的数据项?'
    )
    .then(function () {
      return delPay(rowIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

init()
</script>
<style scoped>
.yzh-indent :deep(.el-drawer__header) {
  background-color: rgba(245, 245, 245, 1);
  margin-bottom: 0;
  padding-bottom: 20px;
}
.search-drawer-retract {
  color: #999999;
}
</style>
